package com.theme.passport.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.view.R;
import com.theme.passport.controller.sys.AbstractController;
import com.theme.passport.entity.SysUserEntity;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.theme.ThemeEntity;
import com.theme.passport.entity.theme.WallpaperEntity;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.service.SysUserService;
import com.theme.passport.service.UserService;
import com.theme.passport.service.theme.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


/**
 * 首页
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2020-04-02 09:20:40
 */
@RestController
@RequestMapping("home")
public class HomeController extends AbstractController {
    @Autowired
    private UserService userService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ThemeService themeService;

    @Autowired
    private WidgeService widgeService;

    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private SearchHistoryService searchHistoryService;

    @Autowired
    private TradeCollectService tradeCollectService;

    @GetMapping(value = "/statis")
    public R statis(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<>();
        //用户数量:
        QueryWrapper<UserEntity> likeWrapper = new QueryWrapper<>();
        int appUserCount = userService.count(likeWrapper);
        result.put("appUserCount",appUserCount);

        //设计师数量:
        QueryWrapper<SysUserEntity> likeWrapper2 = new QueryWrapper<>();
        likeWrapper2.and(wrapper -> wrapper.eq("user_type", "2"));
        int designerCount = sysUserService.count(likeWrapper2);
        result.put("designerCount",designerCount);

        //主题数量:
        QueryWrapper<ThemeEntity> likeWrapperTheme = new QueryWrapper<>();
        int themeCount = themeService.count(likeWrapperTheme);
        result.put("themeCount",themeCount);

        //组件数量:
        QueryWrapper<WidgeEntity> likeWrapperWidge = new QueryWrapper<>();
        int widgeCount = widgeService.count(likeWrapperWidge);
        result.put("widgeCount",widgeCount);

        //壁纸数量:
        QueryWrapper<WallpaperEntity> likeWrapperWallpaper = new QueryWrapper<>();
        int wallpaperCount = wallpaperService.count(likeWrapperWallpaper);
        result.put("wallpaperCount",wallpaperCount);

        return R.ok().put("data", result);
    }

    @GetMapping(value = "/echarts")
    public R echarts(@RequestParam(value = "dateType", defaultValue = "3") Byte dateType) {

        //用户数据统计
        Map<String, Object> userDataEcharts = userService.queryUserTotals(dateType);

        //交易订单统计
        Map<String, Object> tradeCollectEcharts = tradeCollectService.queryTradeCollectEcharts(dateType);

        //搜索关键词
        Map<String, Object> searchEcharts = searchHistoryService.searchHistoryService(null);

        return R.ok()
                .put("userDataEcharts", userDataEcharts)
                .put("tradeCollectEcharts", tradeCollectEcharts)
                .put("searchEcharts", searchEcharts);
    }

    @GetMapping(value = "/systemInfo")
    public R systemInfo() {
        Map<String, Object> result = new HashMap<>();
        SysUserEntity user = getUser();
        result.put("avatar", user.getAvatar());
        result.put("username", user.getUsername());
        result.put("nickname", user.getNickname());
        result.put("currentDate", new Date());

        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        Properties props = System.getProperties();
        result.put("serverIp", address.getHostAddress());
        result.put("serverOS", props.getProperty("os.name") + " " + props.getProperty("os.arch"));


        return R.ok().put("systemInfo", result);
    }
}
